package net.njcp.channel;

import net.njcp.spi.exception.NoSpiMatchException;
import net.njcp.spi.selector.api.ISelector;
import net.njcp.spi.selector.api.SpiImplWrapper;

import java.util.Map;

/**
 * @ClassName DataSourceSelector
 * @Description: TODO
 * @Author 柳拓
 * @Date 2019/8/6
 * @Version V1.0
 **/
public class DataSourceSelector implements ISelector<DataSource> {
    @Override
    public <K> K selector(Map<String, SpiImplWrapper<K>> map, DataSource conf) throws NoSpiMatchException {
        if (conf == null || conf.getEChannelType() == null) {
            throw new IllegalArgumentException("userDo or userDO#market should not be null!");
        }

        String name = conf.getEChannelType().getName();
        if (map.containsKey(name)) {
            return map.get(name).getSpiImpl();
        }


        throw new NoSpiMatchException("no spiImp matched marked: " + conf.getEChannelType());
    }
}
